<!DOCTYPE html>
<!--suppress ALL-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script th:src="@{/asserts/scripts/jQuery-3.6.0.js}"></script>
</head>
<body>
    <div>
        <nav th:replace="~{commons/tag::topBar}"></nav>
    </div>

    <div>
        <form th:action="@{/product/list}" method="post">
            商品名称:<input type="text" name="pname" th:value="${pname}" />
            <input type="submit" value="搜 索" />
        </form>

        --------
        <a th:href="@{/cart/mycart}">我的购物车</a> -------------<a th:href="@{/order/myorder}">我的订单</a>
    </div>
    <div>
        <table  width="800px" border="1px">
            <tr>
                <th>ID</th>
                <th>名称</th>
                <th>图片</th>
                <th>价格</th>
                <th>品牌</th>
                <th>生产日期</th>
                <th>操作</th>
            </tr>

            <tr th:each="p: ${pageInfo.list}">
                <th th:text="${p.id}">ID</th>
                <th th:text="${p.pname}">名称</th>
                <th>
                    <img th:src="${p.pimg}" />
                </th>
                <th th:text="${p.price}">价格</th>
                <th th:text="${p.bname}">品牌</th>
                <th th:text="${#dates.format(p.createdate,'yyyy-MM-dd')}">生产日期</th>
                <th>
                    <a href="javascript:void(0)" th:onclick="|add(${p.id})|">加入购物车</a>
                </th>
            </tr>
        </table>
    </div>
    <div>
        <a th:href="@{/product/list(now=${pageInfo.prePage},size=${pageInfo.pageSize},pname=${pname})}">上一页</a> &nbsp;&nbsp;<a th:href="@{/product/list(now=${pageInfo.nextPage},size=${pageInfo.pageSize},pname=${pname})}">下一页</a>
    </div>

    <script>
        function add(id){
            //alert(id);
            let url = "../cart/addCart/"+id ;
            $.get(url,function (data){
                if (data == '-1'){
                    //没有登录
                    alert("请先登录!");
                    location.href = "../login.html" ;
                }else if (data == '1') {
                    alert("添加成功!");
                }else {
                    alert("添加失败!")
                }
            })
        }
    </script>
</body>
</html>